#include "gkv.hpp"

int main(int argc, char** argv)
{
	std::vector<std::string> arguments(argv, argv + argc);
	gkv::GKV_File gkvFile;

    if (argc == 1)
    {
    	//std::cout << "Size of four chars: " << sizeof("ABCD") << std::endl;
        std::cout << "Usage: " << arguments[0].c_str() << " [OPTION]" << std::endl;
        std::cout << "Where OPTION is one of the following:" << std::endl;
        std::cout << "-h, --help                       | Display this message" << std::endl;
        std::cout << "-v, --version                    | Show the version of " << gkv::caption << std::endl;
        std::cout << "-c, --create <directory_name>    | Create a new gkv package" << std::endl;
        std::cout << "-cz, --create-z <directory_name> | Create a new compressed gkv package" << std::endl;
        std::cout << "-l, --list <package_filename>    | Lists the contents of a gkv package" << std::endl;
        std::cout << "-x, --extract <package_filename> | Extract the gkv package" << std::endl;
        std::cout << std::endl;
        std::cout << "Note: " << gkv::caption << " only creates packages of files contained in a given" << std::endl;
        std::cout << "      " << "directory. It will recursively package everything in said" << std::endl;
        std::cout << "      " << "directory." << std::endl;
        std::cout << "      " << "* This program does NOT archive individual files." << std::endl;
		std::cout << "      " << "  A directory name must be given." << std::endl;
        std::cout << std::endl;
        std::cout << "Create Example: " << "./gamekiver -c test_dir" << std::endl;
        std::cout << "                " << "Where \"test_dir\" is a directory contained in the" << std::endl;
        std::cout << "                " << "current working directory. The output of this" << std::endl;
        std::cout << "                " << "command will be \"test_dir.gkv\"." << std::endl;
        return 0;
    }

	if (argc <= 2)
	{
		for (unsigned int i = 1; i < arguments.size(); i++)
		{
			// help
			if (arguments[i] == "--help" || arguments[i] == "-h")
			{
                std::cout << "Usage: " << arguments[0].c_str() << " [OPTION]" << std::endl;
                std::cout << "Where OPTION is one of the following:" << std::endl;
                std::cout << "-h, --help                       | Display this message" << std::endl;
                std::cout << "-v, --version                    | Show the version of " << gkv::caption << std::endl;
                std::cout << "-c, --create <directory_name>    | Create a new gkv package" << std::endl;
                std::cout << "-cz, --create-z <directory_name> | Create a new compressed gkv package" << std::endl;
                std::cout << "-l, --list <package_filename>    | Lists the contents of a gkv package" << std::endl;
                std::cout << "-x, --extract <package_filename> | Extract the gkv package" << std::endl;
                std::cout << std::endl;
                std::cout << "Note: " << gkv::caption << " only creates packages of files contained in a given" << std::endl;
                std::cout << "      " << "directory. It will recursively package everything in said" << std::endl;
                std::cout << "      " << "directory." << std::endl;
                std::cout << "      " << "* This program does NOT archive individual files." << std::endl;
                std::cout << "      " << "  A directory name must be given." << std::endl;

                std::cout << std::endl;
                std::cout << "Create Example: " << "./gamekiver -c test_dir" << std::endl;
                std::cout << "                " << "Where \"test_dir\" is a directory contained in the" << std::endl;
                std::cout << "                " << "current working directory. The output of this" << std::endl;
                std::cout << "                " << "command will be \"test_dir.gkv\"." << std::endl;
				return 0;
			}
			// version
			else if (arguments[i] == "--version" || arguments[i] == "-v")
			{
				// The GKV_File class prints this in the constructor, so no need to do it here.
				// This option is only here for completeness.
				//std::cout << gkv::caption.c_str() << " v" << gkv::version.c_str() << std::endl;
				return 0;
			}
			// create
			else if (arguments[i] == "--create" || arguments[i] == "-c")
			{
				// Handled later on... take argument of a valid package.
				std::cout << "Please specify a directory to package." << std::endl;
			}
			// create
			else if (arguments[i] == "--create-z" || arguments[i] == "-cz")
			{
				// Handled later on... take argument of a valid package.
				std::cout << "Please specify a directory to package." << std::endl;
			}
			// list
			else if (arguments[i] == "--list" || arguments[i] == "-l")
			{
				// Handled later on... take argument of a valid package.
				std::cout << "Please specify a package to display a listing." << std::endl;
			}
			// extract
			else if (arguments[i] == "--extract" || arguments[i] == "-x")
			{
				// Handled later on... take argument of a valid package.
				std::cout << "Please specify a package to extract." << std::endl;
			}
			// unknown argument
			else if (arguments[i].substr( 0, 1 ) == "-")
			{
				std::cout <<    "Unknown argument " << arguments[i].c_str() << std::endl <<
                                "Use --help to list all possible arguments" << std::endl;
				return 1;
			}
			// invalid argument
			else if (arguments[1].substr( 0, 1 ) != "-")
			{
				std::cout << "Invalid argument!" << std::endl << "Use --help to list all possible arguments" << std::endl;
				return 1;
			}
		}
	}
	// command create
	if (argc > 2 && (arguments[1] == "--create" || arguments[1] == "-c" ) && !arguments[2].empty())
	{
		std::string outfile;
		outfile = gkvFile.create(arguments[2].c_str());

        if (outfile == "")
        {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Create Failed! Sorry for the inconvenience." << std::endl;
			std::cout << "===========" << std::endl;

            return 0;
        } else {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Create Succeeded: " << outfile.c_str() << std::endl;
			std::cout << "===========" << std::endl;
        }
	}
	// command create zlib compressed
	if (argc > 2 && (arguments[1] == "--create-z" || arguments[1] == "-cz" ) && !arguments[2].empty())
	{
		std::string outfile;
		outfile = gkvFile.createZ(arguments[2].c_str());

        if (outfile == "")
        {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Create Failed! Sorry for the inconvenience." << std::endl;
			std::cout << "===========" << std::endl;

            return 0;
        } else {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Create Succeeded: " << outfile.c_str() << std::endl;
			std::cout << "===========" << std::endl;
        }
	}
	// command extract
	else if (argc > 2 && (arguments[1] == "--extract" || arguments[1] == "-x" ) && !arguments[2].empty())
	{
        if (!gkvFile.extractAll(arguments[2].c_str()))
        {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Extract Failed! Sorry for the inconvenience." << std::endl;
			std::cout << "===========" << std::endl;

            return 0;
        } else {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Extract Succeeded!" << std::endl;
			std::cout << "===========" << std::endl;
        }
	}
	// command extract
	else if (argc > 2 && (arguments[1] == "--list" || arguments[1] == "-l" ) && !arguments[2].empty())
	{
		std::cout << "List is not implemented yet. Call me lazy." << std::endl;
        /*
        if (!gkvFile.extractAll(arguments[2].c_str()))
        {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Extract Failed! Sorry for the inconvenience." << std::endl;
			std::cout << "===========" << std::endl;

            return 0;
        } else {
			std::cout << "===========" << std::endl;
			std::cout << " " << gkv::caption << " Extract Succeeded!" << std::endl;
			std::cout << "===========" << std::endl;
        }
        */
	}

    return 0;
}
